package org.softmed.rest.editor;

import java.lang.reflect.Method;

import org.softmed.rest.editor.comps.SimpleEditor;
import org.softmed.rest.editor.comps.browser.RESTServiceBrowser;
import org.softmed.rest.editor.comps.full.ApplicationEditorFull;
import org.softmed.rest.editor.comps.full.HandlerEditorFull;
import org.softmed.rest.editor.comps.full.HttpMethodEditorFull;
import org.softmed.rest.editor.comps.full.MimeTypeHandlerEditorFull;
import org.softmed.rest.editor.comps.full.ModuleEditorFull;
import org.softmed.rest.editor.comps.full.ParameterEditorFull;
import org.softmed.rest.editor.comps.full.ResourceEditorFull;
import org.softmed.rest.editor.config.Application;
import org.softmed.rest.editor.config.Handler;
import org.softmed.rest.editor.config.HttpMethod;
import org.softmed.rest.editor.config.MimeTypeHandler;
import org.softmed.rest.editor.config.Module;
import org.softmed.rest.editor.config.Parameter;
import org.softmed.rest.editor.config.Resource;

public class ComplexEditorDecider {

	RESTServiceBrowser restAdminURIProvider;

	public ComplexEditorDecider(RESTServiceBrowser restAdminURIProvider) {
		this.restAdminURIProvider = restAdminURIProvider;
	}

	public SimpleEditor getEditor(Object object) throws Throwable {
		SimpleEditor editor = null;
		if (object instanceof Application) {
			// setupHeader(new AppHeader());
			editor = new ApplicationEditorFull(restAdminURIProvider);
		} else if (object instanceof Module) {
			// setupHeader(new AppHeader());
			editor = new ModuleEditorFull(restAdminURIProvider);
		} else if (object instanceof Resource) {
			// setupHeader(new ResourceHeader());
			editor = new ResourceEditorFull(restAdminURIProvider);
		} else if (object instanceof HttpMethod) {
			// setupHeader(new HttpHeader());
			editor = new HttpMethodEditorFull(restAdminURIProvider);
		} else if (object instanceof MimeTypeHandler) {
			// setupHeader(new MimeTypeHeader());
			editor = new MimeTypeHandlerEditorFull(restAdminURIProvider);
		} else if (object instanceof Handler) {
			// setupHeader(new HandlerHeader());
			editor = new HandlerEditorFull(restAdminURIProvider);
		} else if (object instanceof Parameter) {
			// setupHeader(new ParameterHeader());
			editor = new ParameterEditorFull(restAdminURIProvider);
		}

		Method getUri = object.getClass().getMethod("getUri");
		String uri = (String) getUri.invoke(object, null);

		// editor.loadEntity(uri + "?detailed");
		editor.setEntity(object);
		// editor.set
		// editor.refresh();
		editor.setEditable(true);
		return editor;
	}

}
